package com.dcloud.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dcloud.manage.mapper.CollectTaskSqlMapper;
import com.dcloud.manage.service.entity.CollectTask;
import com.dcloud.manage.mapper.CollectTaskMapper;
import com.dcloud.manage.service.ICollectTaskService;
import com.dcloud.manage.service.entity.CollectTaskSql;
import com.dcloud.manage.request.CollectTaskSqlRequest;
import com.dcloud.manage.vo.CollectTaskSqlVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 同步任务表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Service
public class CollectTaskServiceImpl extends ServiceImpl<CollectTaskMapper, CollectTask> implements ICollectTaskService {

    @Autowired
    CollectTaskMapper tTaskMapper;

    @Autowired
    CollectTaskSqlMapper collectTaskSqlMapper;

    /**
     * @param state       状态
     * @param currentDate 时间
     * @return java.util.List<com.dcloud.task.domain.entity.TTaskEntity>
     * @Description 根据状态查询任务
     * @Author shijf
     * @Date 15:33 2022/7/12
     */
    @Override
    public List<CollectTask> queryTaskByState(String state, Date currentDate) {
        return tTaskMapper.selectList(new QueryWrapper<CollectTask>()
                .eq("task_state",state)
                .le("task_next_time",currentDate));
    }

    @Override
    public Page<CollectTaskSqlVO> pageInfo(Page<CollectTaskSqlVO> page, CollectTask collectTask) {
        Page<CollectTaskSqlVO> pageList = tTaskMapper.selectpage(page,collectTask);
        return pageList;
    }

    /**
     * @Description //新增任务
     * @Author shijf
     * @Date 23:07 2023/4/11/011
     * @param collectTaskSqlRequest
     * @return
     **/
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveByTaskSqlReq(CollectTaskSqlRequest collectTaskSqlRequest) {
        CollectTask collectTask = new CollectTask();
        BeanUtils.copyProperties(collectTaskSqlRequest,collectTask);
        tTaskMapper.insert(collectTask);

        CollectTaskSql collectTaskSql = new CollectTaskSql();
        BeanUtils.copyProperties(collectTaskSqlRequest,collectTaskSql);
        collectTaskSql.setTaskTid(collectTask.getTid());
        collectTaskSqlMapper.insert(collectTaskSql);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateByTaskSqlReq(CollectTaskSqlRequest collectTaskSqlRequest) {
        CollectTask collectTask = new CollectTask();
        BeanUtils.copyProperties(collectTaskSqlRequest,collectTask);
        tTaskMapper.updateById(collectTask);

        CollectTaskSql collectTaskSql = new CollectTaskSql();
        BeanUtils.copyProperties(collectTaskSqlRequest,collectTaskSql);
        collectTaskSql.setTid(collectTaskSqlRequest.getTaskSqlTid());
        collectTaskSqlMapper.updateById(collectTaskSql);
    }

    /**
     * @Author shijf
     * @Description //根据条件，不分页查询
     * @Date 16:09 2023/7/24
     * @param collectTask
     * @return java.util.List<com.dcloud.manage.vo.CollectTaskSqlVO>
     **/
    @Override
    public List<CollectTaskSqlVO> listByParams(CollectTask collectTask) {

        List<CollectTaskSqlVO>  list = tTaskMapper.selectByParams(collectTask);
        return list;
    }
}
